﻿using UnityEngine;
using System.Collections;
namespace UI
{
    public class UICopyChapterItem :MonoBehaviour
    {
        public UICopy copy;
        public string chapter_id;
        public UISprite chapterIcon;

        public UILabel chapterNumber;
        public UILabel chapterName;

        public GameObject chapterSelection;
        public UICopyEvent copyEvent;
        public GameObject lockFlag;
        public UIChapterData chapterData;

        //字体
        string chapterNumberTxt;

        public GameObject redTip;

        void Awake()
        {
            chapterNumberTxt = TableTextBlo.GetText( "copy_chaper_num" );
        }

        public void Init(UIChapterData chapterData,UICopy copy)
        {
            this.chapterIcon.spriteName = chapterData.chapter_icon_res;
            this.chapterName.text = chapterData.chapter_name;
            this.chapterNumber.text = string.Format( chapterNumberTxt, chapterData.chapter_seq );
            this.chapter_id = chapterData.chapter_id;
            this.copy = copy;
            this.chapterData = chapterData;

            if ( chapterData.chapter_id == copy.currentSelectionChapterId )
            {
                chapterSelection.SetActive( true );
            }
            else
            {
                chapterSelection.SetActive( false );
            }

            bool opend = UICopyData.CurrentChapterOpened(chapter_id);

            
            lockFlag.SetActive(!opend);
            
            if(!opend)
            {
                redTip.SetActive(false);
            }
            else
            {
                RefreshStar();
            }

        }

        public void RefreshStar()
        {
            CheckRedTip();
        }

        public void OnClick()
        {
            if( !UICopyData.CurrentChapterOpened( chapter_id ) )
            {
                //BoxManager.CreatePopupTextBox( "未开启当前章节！" );
                return;
            }
            copy.OnRefreshChapter( this.chapter_id );
            copy.GetComponent<UICopyEvent>().OnClickChapter();
        }


        void CheckRedTip()
        {
            redTip.SetActive(false);

            if (chapterData.chapter_inst_get_box_0 &&
                chapterData.chapter_inst_get_box_1 && 
                chapterData.chapter_inst_get_box_2)
            {
                redTip.SetActive(false);
            }
            else
            {
                var currStars = UICopyData.GetChapterStar(chapterData.chapter_id);

                if(!chapterData.chapter_inst_get_box_0)
                {
                    var award0 = chapterData.chapter_box_condition[0];
                    if(currStars >= award0)
                    {
                        redTip.SetActive(true);
                    }
                }
                else if(!chapterData.chapter_inst_get_box_1)
                {
                    var award1 = chapterData.chapter_box_condition[1];
                    if(currStars >= award1)
                    {
                        redTip.SetActive(true);
                    }
                }
                else if(!chapterData.chapter_inst_get_box_2)
                {
                    var award2 = chapterData.chapter_box_condition[2];
                    if (currStars >= award2)
                    {
                        redTip.SetActive(true);
                    }
                }
            }
        }
    }
}